package com.hnluchuan.staff.web.controller.admin;

import com.hnluchuan.core.support.Page;
import com.hnluchuan.core.support.Token;
import com.hnluchuan.staff.common.Log;
import com.hnluchuan.staff.dao.UserDAO;
import com.hnluchuan.staff.dto.*;
import com.hnluchuan.staff.model.Education;
import com.hnluchuan.staff.model.EmergencyContact;
import com.hnluchuan.staff.model.Employee;
import com.hnluchuan.staff.model.User;
import com.hnluchuan.staff.service.EmergencyContactService;
import com.hnluchuan.staff.service.EmployeeService;
import com.hnluchuan.staff.service.UserService;
import com.hnluchuan.staff.web.controller.BaseController;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletContext;
import javax.management.Query;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.*;
import javax.servlet.http.*;
@Controller
@RequestMapping(value = "/admin/employee", produces="text/html;charset=UTF-8")
public class AdminEmployeeController extends BaseController{

	@Autowired
	private EmployeeService employeeService;


	@Autowired
	private UserService userService;

	@Autowired
	private EmergencyContactService emergencyContactService;

	@Autowired
	private UserDAO dao;
	@RequestMapping(value = "/detail")
	@Token(save = true)
	public String detail(Map<String, Object> map, Long id) {
		if (id != null) {
			EmployeeDTO dto = employeeService.load(id);
			/**学历管理*/
			EducationDTO educationDTO = new EducationDTO();
			educationDTO.setEmployee(dto);
			List<EducationDTO> educationDTOS = educationService.findAll(educationDTO);
			/**紧急联系人*/
			EmergencyContactDTO emergencyContactDTO = new EmergencyContactDTO();
			emergencyContactDTO.setEmployee(dto);
			List<EmergencyContactDTO> emergencyContactDTOS = emergencyContactService.findAll(emergencyContactDTO);

			/**工作经验管理*/
			ExperienceDTO experienceDTO = new ExperienceDTO();
			experienceDTO.setEmployee(dto);
			List<ExperienceDTO> experienceDTOS = experienceService.findAll(experienceDTO);

			/**家庭成员管理*/
			FamilyDTO familyDTO = new FamilyDTO();
			familyDTO.setEmployee(dto);
			List<FamilyDTO> familyDTOS = familyService.findAll(familyDTO);

			map.put("fa", familyDTOS);
			map.put("ex", experienceDTOS);
			map.put("em", emergencyContactDTOS);
			map.put("ed", educationDTOS);
			map.put("n", dto);
			return "admin/employee/employee_show";
		}else {
			return "admin/employee/employee_detail";
		}
	}
	@RequestMapping(value = "/detail2")
	@Token(save = true)
	public String detail2(Map<String, Object> map, Long id) {
		if (id != null) {
			EmployeeDTO dto = employeeService.load(id);

			map.put("n", dto);
		}
		return "admin/employee/employee_detail2";
	}

	@Token(remove = true)
	@ResponseBody
	@RequestMapping(value = "/save")
	public String save(EmployeeDTO dto) throws Exception {
		if (dto.getId() == null) {
			UserDTO userDTO = new UserDTO();
			userDTO.setUsername(dto.getName());
			userDTO.setNickName("");
			userDTO.setDeleted(true);
			userDTO.setPassword("");
			userDTO.setEmail("");
			userDTO.setType(-1);
			Long _id = userService.create(userDTO);
			userDTO.setId(_id);
			dto.setUser(userDTO);
			/**
			 * 紧急联系人的添加
			 * 通过数组来接受前端传过来的数据
			 */
			Long contartId = employeeService.create(dto);
			EmergencyContactDTO emergencyContactDTO = new EmergencyContactDTO();
			dto.setId(contartId);
			emergencyContactDTO.setEmployee(dto);
			String[] contartname = dto.getContartName();
			for(int i=0;i<contartname.length;i++){
				emergencyContactDTO.setContartName(dto.getContartName()[i]);
				emergencyContactDTO.setContartRelation(dto.getContartRelation()[i]);
				emergencyContactDTO.setContartPhone(dto.getContartPhone()[i]);
				emergencyContactDTO.setContartAddress(dto.getContartAddress()[i]);
				emergencyContactService.create(emergencyContactDTO);
			}
			/**
			 * 添加教育以及培训经验
			 */
			EducationDTO educationDTO = new EducationDTO();
			dto.setId(contartId);
			educationDTO.setEmployee(dto);
			Date[] start=dto.getStart();
			for (int i =0;i<start.length;i++){
				educationDTO.setEducastart(dto.getStart()[i]);
				educationDTO.setEducaend(dto.getEnd()[i]);
				educationDTO.setEducaMajor(dto.getEducaMajor()[i]);
				educationDTO.setEducarecord(dto.getRecord()[i]);
				educationDTO.setCertificate(dto.getCertificate()[i]);
				educationService.create(educationDTO);
			}
			/**
			 * 工作经验
			 */
			ExperienceDTO experienceDTO = new ExperienceDTO();
			dto.setId(contartId);
			experienceDTO.setEmployee(dto);
			for(int i = 0;i<dto.getCompany().length;i++){
				experienceDTO.setCompany(dto.getCompany()[i]);
				experienceDTO.setWork(dto.getWork()[i]);
				experienceDTO.setExposition(dto.getExposition()[i]);
				experienceDTO.setReason(dto.getReason()[i]);
				experienceDTO.setWorkstart(dto.getWorkStart()[i]);
				experienceDTO.setWorkend(dto.getWorkEnd()[i]);
				experienceService.create(experienceDTO);
			}
			/**
			  家庭成员
			 * */
			FamilyDTO familyDTO = new FamilyDTO();
			dto.setId(contartId);
			familyDTO.setEmployee(dto);
			for(int i = 0;i<dto.getFamilyname().length;i++){
				familyDTO.setFamilyname(dto.getFamilyname()[i]);
//				familyDTO.setFamilyRelation(dto.getFamilyRelation()[i]);
				familyDTO.setContact(dto.getContact()[i]);
				familyDTO.setFamilyage(dto.getFamilyage()[i]);
				familyDTO.setFamilycompany(dto.getFamilycompany()[i]);
				familyDTO.setFamilyposition(dto.getFamilyposition()[i]);
				familyService.create(familyDTO);
			}

		} else {
			employeeService.updateAllFields(dto);
			//throw new RuntimeException("请实现更新逻辑，并删除该行");

			EmployeeDTO old = employeeService.load(dto.getId());
			// 确定哪些字段是在详情页面需要修改的，不在详情页修改的，就不需要在这里set值。updated和updatedBy不需要在这里处理，框架会统一处理
			old.setId(dto.getId());
			old.setUser(dto.getUser());
			old.setPosition(dto.getPosition());
			old.setName(dto.getName());
			old.setBirth(dto.getBirth());
			old.setSex(dto.getSex());
			old.setHeight(dto.getHeight());
			old.setWeight(dto.getWeight());
			old.setPlace(dto.getPlace());
			old.setNation(dto.getNation());
			old.setPhone(dto.getPhone());
			//old.setIDCard(dto.getIDCard());
			old.setSchool(dto.getSchool());
			old.setMajor(dto.getMajor());
			old.setEducation(dto.getEducation());
			old.setLocation(dto.getLocation());
			old.setAddress(dto.getAddress());
			old.setWechat(dto.getWechat());
			old.setEmail(dto.getEmail());
			old.setMarry(dto.getMarry());
			//old.setInTime(dto.getInTime());
			old.setStatus(dto.getStatus());
			employeeService.updateAllFields(old);

		}
		return ok();
	}

	@ResponseBody
	@RequestMapping(value = "/delete") 
	public String delete(Long[] ids) throws IOException {
		for (Long id:ids){
			EmployeeDTO dto = employeeService.load(id);
			Long userId = dto.getUser().getId();
			userService.deleteById(userId);//删除关联用户


			//删除紧急联系人
			EmergencyContactDTO emergencyContact = new EmergencyContactDTO();
			Employee employee = new Employee();
			employee.setId(id);
			emergencyContact.setEmployee(employee);
			List<EmergencyContactDTO> emergencyContacts = emergencyContactService.find(emergencyContact,null);
			for (EmergencyContactDTO emergencyContactDTO : emergencyContacts){
				emergencyContactService.deleteById(emergencyContactDTO.getId());
			}


			//删除教育及培训情况
			EducationDTO educationDTO = new EducationDTO();
			educationDTO.setEmployee(employee);
			List<EducationDTO> educationDTOS = educationService.find(educationDTO,null);
			for (EducationDTO education:educationDTOS){
				educationService.deleteById(education.getId());
			}
			//删除工作经验
			ExperienceDTO experienceDTO = new ExperienceDTO();
			experienceDTO.setEmployee(employee);
			List<ExperienceDTO> experienceDTOS = experienceService.find(experienceDTO,null);
			for (ExperienceDTO education:experienceDTOS){
				experienceService.deleteById(education.getId());
			}
			//家庭成员
			FamilyDTO familyDTO = new FamilyDTO();
			familyDTO.setEmployee(employee);
			List<FamilyDTO> familyDTOS =familyService.find(familyDTO,null);
			for (FamilyDTO education:familyDTOS){
				familyService.deleteById(education.getId());
			}


		}
		employeeService.deleteByIds(ids);
		return ok();
	}


	@RequestMapping(value = "/list")
	public String list(EmployeeDTO dto, Boolean search, Map<String, Object> map, Page page) {
		List<EmployeeDTO> dtos = employeeService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		map.put("search", search);
		return "admin/employee/employee_list";
	}
	@RequestMapping(value = "/list2")
	public String list2(EmployeeDTO dto, Boolean search, Map<String, Object> map, Page page) {
		List<EmployeeDTO> dtos = employeeService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		map.put("search", search);
		return "admin/employee/employee_list2";
	}
	@RequestMapping(value = "/search")
	public String search(EmployeeDTO dto, Map<String, Object> map, Page page) {
		List<EmployeeDTO> dtos = employeeService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		return "admin/employee/employee_search";
	}
}
